function run_test262_release()
{
    cd $source_dir
    ./build.sh --product-name rk3568 --build-target ark_js_host_linux_tools_packages --build-target ark_ts2abc_build
    cd arkcompiler/ets_frontend
    python3 test262/run_test262.py --es2021 all --timeout 180000  --libs-dir ../../out/rk3568/clang_x64/ark/ark:../../out/rk3568/clang_x64/ark/ark_js_runtime:../../out/rk3568/clang_x64/thirdparty/icu:../../prebuilts/clang/ohos/linux-x86_64/llvm/lib --ark-frontend-binary=../../out/rk3568/clang_x64/ark/ark/build/src/index.js --ark-tool=../../out/rk3568/clang_x64/ark/ark_js_runtime/ark_js_vm --ark-aot-tool=../../out/rk3568/clang_x64/ark/ark_js_runtime/ark_aot_compiler --ark-aot > $test_dir/log/aot_test262_${time_stamp}_release.log
}

function run_test262_debug()
{
    cd $source_dir
    # speed up for debug test
    sed -i 's/\([^a-z]\)-O0/\1-O2/' $source_dir/arkcompiler/ets_runtime/BUILD.gn

    ./build.sh --product-name rk3568 --build-target ark_js_host_linux_tools_packages --build-target ark_ts2abc_build --gn-args is_debug=true
    cd arkcompiler/ets_frontend
    python3 test262/run_test262.py --es2021 all --timeout 180000 --libs-dir ../../out/rk3568/clang_x64/lib.unstripped/clang_x64/ark/ark:../../out/rk3568/clang_x64/lib.unstripped/clang_x64/ark/ark_js_runtime:../../out/rk3568/clang_x64/lib.unstripped/clang_x64/thirdparty/icu:../../prebuilts/clang/ohos/linux-x86_64/llvm/lib --ark-tool=../../out/rk3568/clang_x64/exe.unstripped/clang_x64/ark/ark_js_runtime/ark_js_vm --ark-frontend-binary=../../out/rk3568/clang_x64/ark/ark/build/src/index.js --ark-aot-tool=../../out/rk3568/clang_x64/ark/ark_js_runtime/ark_aot_compiler --ark-aot > $test_dir/log/aot_test262_${time_stamp}_debug.log
}

function run_test262_arm64()
{
    cd $source_dir
    sed -i 's/enable_bytrace = true/enable_bytrace = false/g' $source_dir/arkcompiler/ets_runtime/js_runtime_config.gni
    sed -i 's/enable_hitrace = true/enable_hitrace = false/g' $source_dir/arkcompiler/ets_runtime/js_runtime_config.gni
    sed -i 's/enable_hilog = true/enable_hilog = false/g' $source_dir/arkcompiler/ets_runtime/js_runtime_config.gni
    sed -i 's/enable_dump_in_faultlog = true/enable_dump_in_faultlog = false/g' $source_dir/arkcompiler/ets_runtime/js_runtime_config.gni
    ./build.sh --product-name rk3568 --gn-args use_musl=true --build-target ark_js_vm --build-target ark_js_runtime --build-target ark_js_host_linux_tools_packages --build-target ark_ts2abc_build --target-cpu arm64
    cd arkcompiler/ets_frontend
    python3 test262/run_test262.py --es2021 all --timeout 180000 --libs-dir=../../out/rk3568/ark/ark/:../../out/rk3568/ark/ark_js_runtime/:../../prebuilts/clang/ohos/linux-x86_64/llvm/lib/aarch64-linux-ohos/c++/:../../out/rk3568/utils/utils_base/:../../out/rk3568/thirdparty/icu:../../out/rk3568/securec/thirdparty_bounds_checking_function:../../out/rk3568/hiviewdfx/faultloggerd:../../out/rk3568/thirdparty/bounds_checking_function:../../out/rk3568/hiviewdfx/hilog_native:../../out/rk3568/startup/init:../../out/rk3568/thirdparty/cjson:../../out/rk3568/lib.unstripped/common/dsoftbus:../../out/rk3568/security/selinux:../../out/rk3568/hiviewdfx/hitrace_native/:../../out/rk3568/communication/ipc/:../../out/rk3568/distributedschedule/samgr_standard:../../out/rk3568/security/access_token:../../out/rk3568/communication/dsoftbus:../../out/rk3568/startup/startup_l2/:../../out/rk3568/security/huks/:../../out/rk3568/clang_x64/ark/ark/:../../out/rk3568/clang_x64/thirdparty/icu/:../../out/rk3568/clang_x64/ark/ark_js_runtime --ark-frontend-binary=../../out/rk3568/clang_x64/ark/ark/build/src/index.js --ark-tool=../../out/rk3568/ark/ark_js_runtime/ark_js_vm --ark-aot-tool=../../out/rk3568/clang_x64/ark/ark_js_runtime/ark_aot_compiler --ark-aot --ark-arch=aarch64 --ark-arch-root=aarch64 > $test_dir/log/aot_test262_${time_stamp}_arm64.log
}

function run_5000class()
{
    cd $test_dir/5000Classes
    ./runAotCommand.sh import5000.js &> $test_dir/aot_5000class.log 
    ./runAotCommand.sh import5000_new.js &> $test_dir/aot_5000class_new.log
}

function run_benchmark()
{
    cd $source_dir/arkcompiler/ets_runtime
    python test/runtest.py $test_dir/benchmark_octane/lib --all --product rk3568 --timeout 2400 &> $test_dir/log/aot_benchmark_${time_stamp}.log
}

function reset_code()
{
    cd $source_dir/arkcompiler/ets_runtime
    git reset --hard HEAD
    cd $source_dir/arkcompiler/ets_frontend
    git reset --hard HEAD
}

function get_test262_result()
{
    cd $test_dir
    tail -n 8 log/aot_test262_${time_stamp}_$1.log >> aot_summary_$1.txt
    sed -i 's/PASS test262.*//g' log/aot_test262_${time_stamp}_$1.log
    sed -i '/^$/d' log/aot_test262_${time_stamp}_$1.log
    
    ran=$(grep "Ran [0-9]\+" aot_summary_$1.txt | awk -F ' ' '{print $2}')
    sed -i "s#ran_$1: 0#Ran: $ran#" aot_report.html
    
    passed=$(grep "[0-9]\+ passed" aot_summary_$1.txt | awk -F ' ' '{print $1}')
    sed -i "s#passed_$1: 0#Passed: $passed#" aot_report.html
    
    failed=$(grep "[0-9]\+ failed" aot_summary_$1.txt | awk -F ' ' '{print $1}')
    sed -i "s#failed_$1: 0#Failed: $failed#" aot_report.html
    
    time=$(grep "used time.*[0-9:.]\+" aot_summary_$1.txt | awk -F ' ' '{print $4}')
    sed -i "s#used_time_$1: 0#Used Time: $time#" aot_report.html

    rm -rf aot_summary_$1.txt
}

function get_5000class_result_string()
{
    ret=$(grep -i "Segmentation fault" $1)
    if [ -n "$ret" ]; then
        result="$2: Segmentation fault"
    else
        result=$(cat $1)
    fi
    
    echo $result
}

function get_5000class_result()
{
    cd $test_dir
    build_5000class=$(get_5000class_result_string 5000class.txt import5000.js)
    sed -i "s#5000classes_time_asmint#$build_5000class#" aot_report.html
    build_5000class=$(get_5000class_result_string 5000class_new.txt import5000_new.js)
    sed -i "s#5000classes_new_time_asmint#$build_5000class#" aot_report.html

    build_5000class=$(get_5000class_result_string aot_5000class.log import5000.js)
    sed -i "s/5000classes_time_aot/$build_5000class/" aot_report.html

    build_5000class=$(get_5000class_result_string aot_5000class_new.log import5000_new.js)
    sed -i "s/5000classes_new_time_aot/$build_5000class/" aot_report.html
}

function get_benchmark_asm_score()
{
    asm_score=$(grep "$1.*[0-9]\+" benchmark.txt | awk -F ' ' '{print $3}' | head -1)
    sed -i "s#$1_score_asm#$asm_score#" build_aot_benchmark_table.wip
}

function get_benchmark_aot_score()
{
    old_score=$(grep "$1.*[0-9]\+" $2 | awk -F ' ' '{print $3}' | head -1)
    new_score=$(grep "$1.*[0-9]\+" log/aot_benchmark_${time_stamp}.log | awk -F ' ' '{print $3}' | head -1)
    if [ -n "$old_score" -a -n "$new_score" ]; then
        percent=$(python -c "print(int(100*($new_score-$old_score)/$old_score))")
    fi
    if [ -z "$old_score" ]; then
        old_score="FAIL"
    fi
    if [ -z "$new_score" ]; then
        new_score="FAIL"
    fi
    sed -i "s#$1_score_aot#$new_score  ($percent%)#" build_aot_benchmark_table.wip
}

function get_benchmark_result()
{
    cd $test_dir
    cp build_aot_benchmark_table.template build_aot_benchmark_table.wip

    get_benchmark_asm_score "Crypto"
    get_benchmark_asm_score "EarleyBoyer"
    get_benchmark_asm_score "RayTrace"
    get_benchmark_asm_score "Splay"
    get_benchmark_asm_score "SplayLatency"
    get_benchmark_asm_score "Box2D"
    get_benchmark_asm_score "NavierStokes"
    get_benchmark_asm_score "Richards"
    get_benchmark_asm_score "DeltaBlue"
    get_benchmark_asm_score "RegExp"
    get_benchmark_asm_score "Mandreel"
    get_benchmark_asm_score "MandreelLatency"
    get_benchmark_asm_score "PdfJS"
    get_benchmark_asm_score "Typescript"

    # get last log
    old=""
    new=""
    for log in $(ls log/aot_benchmark_*.log)
    do
        old=$new
        new=$log
        if [ "$new" == "log/aot_benchmark_${time_stamp}.log" ]; then
            break
        fi
    done
    get_benchmark_aot_score "Crypto" $old
    get_benchmark_aot_score "EarleyBoyer" $old
    get_benchmark_aot_score "RayTrace" $old
    get_benchmark_aot_score "Splay" $old
    get_benchmark_aot_score "SplayLatency" $old
    get_benchmark_aot_score "Box2D" $old
    get_benchmark_aot_score "NavierStokes" $old
    get_benchmark_aot_score "Richards" $old
    get_benchmark_aot_score "DeltaBlue" $old
    get_benchmark_aot_score "RegExp" $old
    get_benchmark_aot_score "Mandreel" $old
    get_benchmark_aot_score "MandreelLatency" $old
    get_benchmark_aot_score "PdfJS" $old
    get_benchmark_aot_score "Typescript" $old
    cat build_aot_benchmark_table.wip >> aot_report.html
    rm -rf build_aot_benchmark_table.wip
}

function send_mail()
{
    cd $test_dir
    # MAX: 10MB
    MAX_FILE_SIZE=10485760
    file_size_release=$(stat --format=%s log/aot_test262_${time_stamp}_release.log)
    file_size_debug=$(stat --format=%s log/aot_test262_${time_stamp}_debug.log)
    file_size_arm64=$(stat --format=%s log/aot_test262_${time_stamp}_arm64.log)
    file_size_benchmark=$(stat --format=%s log/aot_benchmark_${time_stamp}.log)
    file_size=$(expr $file_size_release + $file_size_debug + $file_size_arm64 + $file_size_benchmark)
    if [ $file_size -gt $MAX_FILE_SIZE ]; then
        cat aot_report.html |  mutt -s "openharmony ark aot test report(log too large) - $time_stamp" -e "set content_type=text/html" -- $(cat recipients)
    else
        cat aot_report.html | mutt -s "openharmony ark aot test report - $time_stamp" -e "set content_type=text/html" -a log/aot_test262_${time_stamp}_release.log	-a log/aot_test262_${time_stamp}_debug.log -a log/aot_test262_${time_stamp}_arm64.log -a log/aot_benchmark_${time_stamp}.log -- $(cat recipients)
    fi
}

#######################################################################################################################
time_stamp=$(date +%m%d%H%M)
if [ -n "$1" ]; then
    time_stamp=$1
fi

test_dir=/usr1/EcmaCI/shell/262Test
source_dir=/usr1/EcmaCI/openharmony-master
rm -rf $test_dir/aot_report.html
mkdir -p $test_dir/log

cat $test_dir/header.preset >> $test_dir/aot_report.html
# get commit info
cat $test_dir/build_commit_table.wip >> $test_dir/aot_report.html

# only report test for debug use
sed -i 's/--asm-interpreter=1/--asm-interpreter=true/' $source_dir/arkcompiler/ets_frontend/test262/run_sunspider.py
if [ -z "$1" ]; then
    run_test262_arm64
    run_test262_debug
    # do not move up, for benchmark use afterwards
    run_test262_release
    run_5000class
    run_benchmark
    reset_code
fi

cat $test_dir/build_aot_summary_table.template >> $test_dir/aot_report.html
get_test262_result release
get_test262_result debug
get_test262_result arm64
get_5000class_result
get_benchmark_result
cat $test_dir/tail.preset >> $test_dir/aot_report.html
send_mail